<?php

namespace Trawind\Remotes\Repositories\Storage\Oss;

use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Trawind\Remotes\BaseRepository;
use Trawind\Remotes\Contracts\Repositories\Storage\Oss\AttachmentsRepository;
use Trawind\Basics\Enums\ServiceDomain;

/**
 * Class AttachmentsRepositoryEloquent.
 *
 * @package namespace  Trawind\Remotes\Repositories\Storage\Oss;
 */
class AttachmentsRepositoryEloquent extends BaseRepository implements AttachmentsRepository
{


    public $baseRoute = '/attachments';


    /**
     * AttachmentsRemoteEloquent constructor.
     * @param  Request  $request
     */
    public function __construct(Request $request)
    {
        $tenantId = $request->get('tenant_id') ?: null;
        $this->domain = ServiceDomain::STORAGE_DOMAIN($tenantId);
    }

    public function create(array $attributes)
    {
        return $this->setUrl($this->baseRoute)
            ->setClientOptions(['form_params' => $attributes])
            ->result('POST');
    }

    public function update(array $attributes, $id)
    {
        // TODO: Implement update() method.
    }

    public function find($id, $columns = ['*'])
    {
        // TODO: Implement find() method.
    }

    /**
     * @param  string[]  $columns
     * @return mixed|void
     */
    function all($columns = ['*'])
    {
        return $this->setUrl($this->baseRoute)->result();
    }

    /**
     * @param $limit
     * @return mixed|null
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getAttachmentsList($limit)
    {
        $this->params['pageSize'] = $limit;
        return $this->setUrl($this->baseRoute)->setClientOptions(['query' => $this->params])->result();
    }

    /**
     * @param $filter
     * @return mixed|null
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function createAttachment($filter)
    {
        return $this->setUrl($this->baseRoute)
            ->setClientOptions(['form_params' => $filter])
            ->result('POST');
    }

    /**
     * @param $filter
     * @param $id
     * @return mixed|null
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function updateAttachment($filter, $id)
    {
        return $this->setUrl($this->baseRoute.'/'.$id)
            ->setClientOptions(['form_params' => $filter])
            ->result('PUT');
    }

    /**
     * @param $filter
     * @return mixed|null
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function deleteAttachmentsByBusiness($filter)
    {
        return $this->setUrl('/attachments/by_business')
            ->setClientOptions(['form_params' => $filter])
            ->result('DELETE');
    }

    /**
     * @param $filter
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function batchUpdateAttachmentByBusiness($filter)
    {
        $returnData = $this->setUrl('/attachments/by_business')
            ->setClientOptions(['form_params' => $filter])
            ->result('PATCH');
        return Collection::make($returnData);
    }

    /**
     * @param $businessTypeId
     * @param $businessIds
     * @param  null  $type
     * @return mixed|null
     */
    public function getAttachmentsByBusiness($businessTypeId, $businessIds, $type = null)
    {
        $search = "business_type_id:{$businessTypeId};business_id:".implode(',', $businessIds);
        $searchFields = 'business_type_id:=;business_id:in';
        is_null($type) or $search .= ';type:'.$type;
        is_null($type) or $searchFields .= ';type:=';
        $attachments = $this->getListBySearch([
            'search' => $search,
            'searchFields' => $searchFields,
            'include' => 'object',
            'with' => 'object',
            'searchJoin' => 'and',
            'pageSize' => 0,
            'source' => 'remote'
        ]);

        return array_column($attachments['data'], null, 'business_id');
    }

    /**
     * @param $filter
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function batchDeleteAttachmentByBusiness($filter)
    {
        return $this->setUrl('/attachments/batch_by_business')
            ->setClientOptions(['form_params' => $filter])
            ->result('DELETE');
    }

    /**
     * @param $filter
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function batchStoreAttachment($filter)
    {
        return $this->setUrl('/attachments/batch')
            ->setClientOptions(['form_params' => $filter], false)
            ->result('POST');
    }
}
